/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

/**
 *
 * @author org.@le
 */
@NamedQueries(value={@NamedQuery(name = "selectTopics", query = "SELECT c FROM Topic c"),
@NamedQuery(name = "topicsForumInv", query = "SELECT a FROM Topic a WHERE a.forum=:forum ORDER BY a.id DESC"),
@NamedQuery(name="topicsForum", query="SELECT a FROM Topic a WHERE a.forum=:forum")})


@Entity
public class Topic implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String type;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date creationDate;
    private String subject;
    @OneToMany(mappedBy = "topic", cascade = CascadeType.REMOVE)
    private List<Message> messages;
    @ManyToOne
    private Forum forum;
    @ManyToOne
    private Nick handle;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public List<Message> getMessages() {
        if (messages == null) {
            messages = new ArrayList<Message>();
        }
        List<Message> mensajes = new ArrayList<Message>();
        for (Message m : messages) {
            if (m.getMessage() == null) {
                mensajes.add(m);
            }
        }
        return mensajes;
    }

    public List<Message> getAllMessages() {
        if (messages == null) {
            messages = new ArrayList<Message>();
        }
        return messages;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    public Forum getForum() {
        return forum;
    }

    public void setForum(Forum forum) {
        this.forum = forum;
    }
    private transient int d = 0;

    public void RecorrerArbol(Message m) {
        if (m.getMessages().isEmpty()) {
            if (m.getLevel() > getD()) {
                setD(m.getLevel());
            }
            return;
        }
        for (Message me : m.getMessages()) {
            if (me.getLevel() > getD()) {
                setD(me.getLevel());
            }
            RecorrerArbol(me);
        }

    }

    public int getDepth() {

        for (Message mess : getMessages()) {
            RecorrerArbol(mess);
        }
        //System.out.println("profundidad: "+getD());
        return getD();
    }

    public int getD() {
        return d;
    }

    public void setD(int d) {
        this.d = d;
    }

    public Nick getHandle() {
        return handle;
    }

    public void setHandle(Nick handle) {
        this.handle = handle;
    }
}
